package com.owen.he.jee.dao.impl.ibatis;

import java.io.InputStream;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.engine.execution.SqlExecutor;
import com.owen.he.jee.dao.impl.ibatis.support.SqlMapClientDaoSupport;
import com.owen.he.jee.dao.impl.ibatis.support.SqlMapClientTemplate;


/**
 * 
 * com.tiancom.pas.framework.ibatis
 * <br>
 * 说明：把LimitSqlExecutor注入ibatis中。
 * 我们是通过spring来使用ibatis的，所以在我们的dao基类中执行注入<br>
 * <br>
 * @author: meteor
 * 创建时间 2007-3-21 上午09:47:17
 */
public class BaseiBatisDao extends SqlMapClientDaoSupport implements ApplicationContextAware
   {
	
	private String sqlClient="sqlMapClient_";

    private SqlExecutor sqlExecutor;
   
    public SqlExecutor getSqlExecutor() {
        return sqlExecutor;
    }

    public void setSqlExecutor(SqlExecutor sqlExecutor) {
        this.sqlExecutor = sqlExecutor;
    }

    public void setEnableLimit(boolean enableLimit) {
        if (sqlExecutor instanceof LimitSqlExecutor) {
            ((LimitSqlExecutor) sqlExecutor).setEnableLimit(enableLimit);
        }
    }
    
    /**
     * 执行注入
     * @throws Exception
     */
    public void initialize() throws Exception {
        if (sqlExecutor != null) {
            SqlMapClient sqlMapClient = getSqlMapClientTemplate().getSqlMapClient();
            
           /*if (sqlMapClient instanceof ExtendedSqlMapClient) {
                //通过反射绕过java的访问控制
            	ReflectUtil.setFieldValue(((ExtendedSqlMapClient) sqlMapClient).getDelegate(), "sqlExecutor", SqlExecutor.class, sqlExecutor);
            }*/
        	//SqlMapClient sqlMapClient = getSqlMapClientTemplate().getSqlMapClient();
        	 //通过反射绕过java的访问控制
            if (sqlMapClient instanceof DySqlMapClient) {
            	ReflectUtil.setFieldValue(((DySqlMapClient) sqlMapClient).getDelegate(), "sqlExecutor", SqlExecutor.class,sqlExecutor);
            	//ReflectUtil.setFieldValue(((ExtendedSqlMapClient) sqlMapClient).getDelegate(), "sqlExecutor", SqlExecutor.class, sqlExecutor);
            	//((DySqlMapClient) sqlMapClient).setDelegate(sqlExecutor);
            }
        }
    }
    
    public void reParseSqlMap(InputStream is){
		SqlMapClient sqlMapClient = getSqlMapClientTemplate().getSqlMapClient();
		
		
		if (sqlMapClient instanceof DySqlMapClient) {
			((DySqlMapClient)sqlMapClient).reParseSqlMap(is);
		}
	}

    protected ApplicationContext context;

	public ApplicationContext getContext() {
		return context;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		// TODO Auto-generated method stub
		this.context = applicationContext;
	}
	
	public SqlMapClientTemplate getSqlMapClientTemplate(String db){
		
		SqlMapClient client = (SqlMapClient) getContext().getBean(sqlClient+db);
		
		SqlMapClientTemplate sqlMapClientTemplate = new SqlMapClientTemplate();
		sqlMapClientTemplate.setSqlMapClient(client);
		return sqlMapClientTemplate;
	}
    
    
    
}
